/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.business;

import entities.Curriculum;
import entities.Semester;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author HoangHai
 */
@Stateless
@LocalBean
public class semesterBean {
    @PersistenceContext(unitName = "DeclarationBoard-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public List<Semester> allSemester(){
        List<Semester> semesters = em.createNamedQuery("Semester.findAll").getResultList();
        return semesters;
    }
    
    public Semester findSemByID(int id){
        Semester semesters = (Semester) em.createNamedQuery("Semester.findBySeID").setParameter("seID", id).getSingleResult();
        return semesters;
    }
    public List<Semester> findSemByCurID(int id){
        List<Semester> semesters = em.createNamedQuery("Semester.findByCuID").setParameter("cuID", id).getResultList();
        return semesters;
    }
    
    public boolean addSemester(Semester s){
        boolean flag;
        try {
            em.persist(s);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
    
    public boolean updateSemester(Semester s){
        boolean flag;
        try {
            em.merge(s);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
    
    public boolean delSemester(int id){
        boolean flag;
        try {
            Semester s = findSemByID(id);
            em.remove(s);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}
